package com.boot.service.impl;

import com.boot.mapper.CompensateRecordsMapper;
import com.boot.pojo.CompensateRecords;
import com.boot.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ScheduleCompensate {
    @Autowired
    private CompensateRecordsMapper compensateRecordsMapper;

    @Autowired
    private PaymentService paymentService;


//    @Scheduled(cron = "0 0 * * * ?")
    @Scheduled(fixedRate = 3600000)
    public void scheduleCompensate() throws InterruptedException {
        int pageNo = 0;
        int pageSize = 100;
        System.out.println("定时补偿服务执行");

        while (true) {
            List<CompensateRecords> lists = compensateRecordsMapper.selectMyPage(pageSize, pageNo * pageSize);
            if (lists.isEmpty()) break;
            for (CompensateRecords record : lists) {
                try {
                    paymentService.compensateDeduct(record);
                } catch (Exception e) {
                    log.error("补偿失败：recordId={}, reason={}", record.getId(), e.getMessage());
                }
            }
            pageNo++;
            Thread.sleep(2000);
        }
    }
}
